﻿//-----------------------------------------------------------------------
// <copyright file="TagItem.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.XamlUtility
{
    using System.Collections.Generic;

    using Utility;

    /// <summary>
    /// A node on the html parse tree
    /// </summary>
    public class TagItem
    {
        /// <summary>
        /// the list of contained tags
        /// </summary>
        private readonly List<TagItem> containedTags = new List<TagItem>();

        /// <summary>
        /// Initializes a new instance of the TagItem class
        /// </summary>
        /// <param name="text">the html text in this item</param>
        public TagItem(string text)
        {
            this.TextContent = text;
        }

        /// <summary>
        /// Initializes a new instance of the TagItem class
        /// </summary>
        /// <param name="text">the html text in this item</param>
        /// <param name="tag">the tag that wraps it</param>
        /// <param name="tagAttributes">the tag attributes</param>
        public TagItem(string text, string tag, string tagAttributes)
        {
            this.TextContent = text;
            this.Tag = tag;
            this.TagAttributes = StringHelpers.SplitParameters(tagAttributes);
        }

        /// <summary>
        /// Gets or sets the Tag that encloses this content
        /// </summary>
        /// <value>
        /// The tag that enclodes this content
        /// </value>
        public string Tag
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the attributes of the tag
        /// </summary>
        public IDictionary<string, string> TagAttributes
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the text inside this tag
        /// </summary>
        /// <value>
        /// The text content.
        /// </value>
        public string TextContent
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the content broken down into tageed items
        /// </summary>
        /// <value>
        /// The contained tags.
        /// </value>
        public IList<TagItem> ContainedTags
        {
            get 
            {
                return this.containedTags;
            }
        }

        /// <summary>
        /// Display for debug
        /// </summary>
        /// <returns>a string describing the object</returns>
        public override string ToString()
        {
            string result = this.TextContent;
            if (! string.IsNullOrEmpty(this.Tag))
            {
                result += " " + this.Tag;
            }

            if (this.ContainedTags.Count > 0)
            {
                result += " " + this.ContainedTags.Count;
            }

            return result;
        }
    }
}
